/***********************************************************
  ToGraph.h
  The ToGraph class reads the input netlist and create an internal edge list.
  After finishing reading, it writes the edge list to a graph file.

  03-25-2003, G.Shi
  09-11-2008, Zhigang Hao
 ************************************************************/

#ifndef TOGRAPH_H
#define TOGRAPH_H

#include <iostream>
#include <fstream>
#include "utility.h"
#include "RddStruct.h"

using namespace std;

class ToGraph 
{

  public:

    ToGraph();
    ~ToGraph();

    void Parse_Netlist(char *filename); // Parse netlist
    void Report_Netlist_Statistics();

    inline void Save_Netlist_Title(char *title) 
    { NetlistTitle = CopyStr(title); }

    inline int Get_Num_Elems()   { return NumElems; }
    inline int Get_Num_Nodes()   { return NumNodes; }
    inline int Get_Num_Caps()   { return NumCaps; }
    inline int Get_Num_Inds()   { return NumInds; }

    inline T_NODE* Get_SrcNode() { return SrcNode;}
    inline int  Get_Fanout(T_NODE *node)
    { return   node->fanout;  }

    inline T_ELEM  *Get_Elem_List()  { return ElemList; }
    inline T_SRC *Get_Src_List() { return SrcList; }

    inline C_NODE* Get_CTree()  { return CTree;}

    T_NODE* Get_Node_By_Index(int k);

    void Print_All_Elements();
    void Print_All_Nodes();

    void Parse_RLC(char *name, char *n1, char *n2, 
        char *value, char *var_name);
    void Parse_SRC(char *name, char *n1, char *n2, 
        int type, char *val1, char *val2, char *val3,
        char *val4, char *val5, char *val6, char *val7);

    void Calculate_All_Fanout();
    double Lookup_Capacitance(T_NODE *node);
    T_ELEM* Lookup_Capacitor(T_NODE *node);
    T_ELEM* Lookup_Inductor(T_NODE *node);
    void Create_CTree();
    void Create_CTree_r(T_NODE *node, C_NODE *&ctree); // for recursion

    void Print_CTree();
    void Print_CTree_r(C_NODE *ctree);  // for recursion

    void Evaluate_CTree(double *moment);
    void Evaluate_CTree_r(C_NODE *ctree, double *moment);  
    // for recursion

    void Delete_CTree();
    void Delete_CTree_r(C_NODE *ctree); // for recursion
    void Generate_Sample();
    void Get_Hspice_Sample(ifstream &infile);

  private:
    void  Set_Sample_Value(double value, T_ELEM *elem, int cnt);
    void  Get_Sample_Value(T_ELEM *elem, int num, double ratio);
    T_NODE  *Lookup_Node(char *node_name);
    T_NODE  *Lookup_Ind_Node(T_NODE *node);
    void  Insert_Node(T_NODE *node);
    void  Number_Nodes();
    void  Delete_Node_Table();

    T_ELEM  *Lookup_Element(char *elem_name);
    void  Insert_Element(T_ELEM *edge);
    void  Insert_Src(T_SRC *src);

    void  Link_Element_To_Node(T_NODE *node, T_ELEM *elem);
    int  hash(char *name,  int tsize);


    int  NumElems;  // Num of edges in the tree.
    int  NumRess;       // Num of C int the tree
    int  NumCaps;       // Num of C int the tree
    int  NumInds;       // Num of L int the tree
    int  NumSrcs;  // Num of sources
    int  NumNodes;  // Num of nodes in the graph.
    int  NodeTableSize; // size of the node table
    char  *NetlistTitle;  // netlist title
    T_ELEM *ElemList;  // Head of the element list
    T_ELEM *CurElem;  // Points to the current edge
    T_ELEM *CurRes;  // Points to the working resistor
    T_SRC  *SrcList;  // Head of the source list
    T_SRC  *CurSrc;   // Points to the current src
    T_NODE **NodeTable; // node hash table
    T_NODE *SrcNode;  // the positive node of source
    T_NODE *GndNode;  // the ground node
    T_NODE **IndexToNode; // node-index to node mapping
    C_NODE *CTree;   // root of the C_Tree

    ofstream outFile;   // For output to a graph file.
    ifstream inFile;

};

#endif

